﻿using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBACProject.Application;
using System.IO;

namespace RBACProject.WebApi.Controllers
{
    /// <summary>
    /// 验证码 api服务端控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ICaptchaController : ControllerBase
    {
        private readonly ICaptcha _captcha;
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        /// <param name="captcha"></param>
        public ICaptchaController(ICaptcha captcha)
        {
            _captcha = captcha;
        }

        /// <summary>
        /// 获取图片验证码
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Captcha(string id)
        {
            var info = _captcha.Generate(id);
            // 有多处验证码且过期时间不一样，可传第二个参数覆盖默认配置。
            //var info = _captcha.Generate(id,120);
            var stream = new MemoryStream(info.Bytes);
            return File(stream, "image/gif");
        }
    }
}
